package com.ocr.adapter;

import java.text.SimpleDateFormat;
import java.util.List;

import com.ocr.R;
import com.ocr.entities.HistoryItem;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class HistoryAdapter extends ArrayAdapter<HistoryItem>{
	private int resId;
	private List<HistoryItem> historyItems;
	private Context context;
	public HistoryAdapter(Context context, int resource,List<HistoryItem> items) {
		super(context, resource, items);
		// TODO Auto-generated constructor stub
		historyItems=items;
		if(historyItems.size()>0)
		resId=resource;
		this.context=context;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(resId, null);
		}
		TextView txtDate = (TextView) convertView.findViewById(R.id.txt_date);
		TextView txtConent = (TextView) convertView
				.findViewById(R.id.txt_content);
		String contentString = historyItems.get(position).getOCRResult();
		if (contentString.contains("\n")) {
			int pos = contentString.indexOf("\n");
			contentString = contentString.substring(0, pos);
			if (contentString.length() > 40)
				contentString = contentString.substring(0, 39) + "...";
			else
				contentString = contentString + "...";
		}
		if (contentString.length() > 40)
			contentString = contentString.substring(0, 39) + "...";
		txtConent.setText(contentString);
		String dateFormat = (new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"))
				.format(historyItems.get(position).getDateCreate());
		txtDate.setText(dateFormat);
		return convertView;
	}
	
}
